package com.edu.admin.service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysGrade;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.GradeMapper;
import com.edu.admin.vo.GradeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 年级服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class GradeService extends BaseService<GradeMapper, SysGrade> {
	
	/**
	 * 获取年级列表(分页)
	 * 
	 * @param phaseId
	 * @param pageNum
	 * @param pageSize
	 * 
	 * @return PageInfo<GradeVo>
	 */
	public PageInfo<GradeVo> getGradeListPage(Long phaseId, Integer pageNum, Integer pageSize) {
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<GradeVo> gradeList =  this.mapper.getGradeList(phaseId);
		
		PageInfo<GradeVo> pageInfo = new PageInfo<GradeVo>(gradeList);
		
		return pageInfo;
	}
	
	/**
	 * 获取年级列表
	 * 
	 * @param phaseId
	 * 
	 * @return List<GradeVo>
	 */
	public List<GradeVo> getGradeList(Long phaseId) {
		return this.mapper.getGradeList(phaseId);
	}
	
	/**
	 * 获取年级信息
	 * 
	 * @param gradeId
	 * 
	 * @return GradeVo
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public GradeVo getGradeById(Long gradeId) throws IllegalAccessException, InvocationTargetException{
		
		GradeVo gradeVo = this.mapper.getGradeByGradeId(gradeId);
		
		return gradeVo;
	}
	
	/**
	 * 获取年级信息
	 * 
	 * @param phaseId
	 * @param yearNum
	 * 
	 * @return GradeVo
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public GradeVo getGradeByPhaseIdAndYearNum(Long phaseId,Integer yearNum) throws Exception{
		
		GradeVo gradeVo = this.mapper.getGradeByPhaseIdAndYearNum(phaseId,yearNum);
		
		return gradeVo;
	}
	
	/**
     * 年级重复性检查
     * 
     * @param SysGrade grade
     * 
     * @return List<GradeVo>
     * @throws Exception
     */
    public List<GradeVo> CheckGrade(SysGrade grade) throws Exception {
    	
    	return this.mapper.findGradeByGradeName(grade.getPhaseId(),grade.getGradeId(), grade.getGradeName().trim());
  
    }
	
	/**
	 * 年级保存
	 * 
	 * @param SysGrade grade
	 * 
	 * @return
	 * @throws Exception 
	 */
	public Map<String, Object> saveGrade(SysGrade grade) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<GradeVo> gradeList = CheckGrade(grade);
		
		if (gradeList!=null && gradeList.size()>0)
		{
			throw new SysException(ExceptionEnum.GRADENAMEREPEAT);
		}

		Long gradeId = grade.getGradeId();

		if (gradeId == null) {
			
			insert(grade);

			gradeId = grade.getGradeId();


		} else {
			
			updateByPrimaryKeySelective(grade);

		}
		
		result.put("gradeId", gradeId);

		return result;

	}
	
	/**
	 * 删除年级
	 * 
	 * @param gradeId
	 * 
	 * @return int
	 */
	public int deleteGrade(Long gradeId) {
		
		if (gradeId != null) {
			return this.deleteByPrimaryKey(gradeId);

		}
		
		return 0;	

	}


}
